package com.sunjy.demo1.configuration;

import com.sunjy.demo1.config.RedissonConfig;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @Auther: sunjy
 * @Date: 2018/10/18 17:26
 * @Description:
 */
@Configuration
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedissonConfig.class)
public class RedissonAutoConfiguration {

    @Autowired
    private RedissonConfig redissonConfig;


    /**
     * 单机模式自动装配
     * @return
     */
    @Bean
    @ConditionalOnProperty(name="redisson.address")
    RedissonClient redissonSingle() {
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(redissonConfig.getAddress())
                .setTimeout(redissonConfig.getTimeout())
                .setConnectionPoolSize(redissonConfig.getConnectionPoolSize())
                .setConnectionMinimumIdleSize(redissonConfig.getConnectionMinimumIdleSize());

        if(redissonConfig.getPassword() != null && !"".equals(redissonConfig.getPassword())) {
            serverConfig.setPassword(redissonConfig.getPassword());
        }

        return Redisson.create(config);

    }
}
